package com.hxzon.netty.statistics.util;

import java.util.Date;

import org.apache.tapestry5.json.JSONObject;
import org.hxzon.util.DateFormatUtil;

import com.hxzon.netty.statistics.model.LoginDetail;
import com.hxzon.netty.statistics.model.base.BaseStatisticsModel;
import com.hxzon.netty.statistics.service.StatisticsClientService;

public class StatisticsClientUtil {

    public static final String MessageType_Login = "1";
    public static final String MessageType_Logout = "2";
    public static final String MessageType_OperClick = "3";
    public static final String MessageType_Usage = "4";
    public static final String MessageType_UpdateClient = "5";

    public static void dealwithMessage(String jsonStr, String remoteIp, StatisticsClientService service) {
        JSONObject json = new JSONObject(jsonStr);
        json.put("clientIp", remoteIp);
        String messageType = json.getString("messageType");
        if (MessageType_Login.equals(messageType) || MessageType_Logout.equals(messageType)) {
            dealwithMessageForLogin(json, service);
        }
    }

    private static void dealwithCommonProperty(JSONObject json, BaseStatisticsModel model, StatisticsClientService service) {
        model.setMessageType(json.getString("messageType"));
        model.setClientIp(json.getString("clientIp"));
        model.setClientVersion(json.getString("clientVersion"));
        model.setRecordTime(DateFormatUtil.formatToUniteTime(new Date()));
    }

    private static void dealwithMessageForLogin(JSONObject json, StatisticsClientService service) {
        LoginDetail detail = new LoginDetail();
        dealwithCommonProperty(json, detail, service);
        detail.setLoginUuid(json.getString("loginUuid"));
        //service.insertLoginDetail(detail);
    }

}
